
using System;
using UnityEngine;

namespace XFGameFramework.TweenSystem
{
     
    /// <summary>
    /// Animator动画拓展
    /// </summary>
    public static class AnimatorExtensions 
    {

        /// <summary>
        /// 渐变LayerWeight(根据LayerName)
        /// </summary>
        /// <param name="animator">Animator对象</param>
        /// <param name="layerName">层级名称</param>
        /// <param name="target">目标权重</param>
        /// <param name="duration">持续时间</param>
        /// <param name="loop">循环模式</param>
        /// <param name="ease">缓动曲线</param>
        /// <param name="curve">自定义的缓动曲线</param>
        /// <param name="ingoreTimeScale">是否忽略时间缩放</param>
        /// <param name="delay">延迟</param>
        /// <param name="loopCount">循环次数</param>
        /// <returns></returns>
        public static TweenerCore<float> TweenLayerWeight(this Animator animator, string layerName, float target, float duration, LoopMode loop = LoopMode.Once, Ease ease = Ease.Linear, AnimationCurve curve = null, bool ingoreTimeScale = false, float delay = 0,int loopCount = -1) 
        {
            int layerIndex = animator.GetLayerIndex(layerName);
            return animator.TweenLayerWeight(layerIndex,target,duration,loop,ease,curve,ingoreTimeScale,delay,loopCount);
        }

        /// <summary>
        ///  渐变LayerWeight(根据LayerIndex)
        /// </summary>
        /// <param name="animator">Animator对象</param>
        /// <param name="layerIndex">层级下标</param>
        /// <param name="target">目标权重</param>
        /// <param name="duration">持续时间</param>
        /// <param name="loop">循环模式</param>
        /// <param name="ease">缓动曲线</param>
        /// <param name="curve">自定义的缓动曲线</param>
        /// <param name="ingoreTimeScale">是否忽略时间缩放</param>
        /// <param name="delay">延迟</param>
        /// <param name="loopCount">循环次数</param>
        /// <returns></returns>
        public static TweenerCore<float> TweenLayerWeight(this Animator animator,int layerIndex, float target, float duration, LoopMode loop = LoopMode.Once, Ease ease = Ease.Linear, AnimationCurve curve = null, bool ingoreTimeScale = false, float delay = 0, int loopCount = -1)
        { 
            float weight_from = animator.GetLayerWeight(layerIndex);

            Action<TweenerCore<float>> callback = (TweenerCore<float> t) =>
            { 
                animator.SetLayerWeight(layerIndex, t.Value);
            };

            return TweenManager.ComponentTween(animator, weight_from, target, duration, loop, ease, curve, ingoreTimeScale, callback, delay,0,loopCount);
        } 
    }

}

